package com.kang.controller;

import java.io.File;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.SupplierAnnex;
import com.kang.service.SupplierAnnexService;
import com.kang.until.FileUtil;

/**
 * 供应商附件控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("supplierannex")
public class SupplierAnnexController extends com.kang.controller.Controller {
	@Resource
	private SupplierAnnexService ss;

	/**
	 * 查看供应商全部附件
	 * 
	 * @param supplierid
	 *            供应商ID
	 * @return 返回json字符串
	 */
	@RequestMapping("annexlist")
	@ResponseBody
	public String listPaging(Integer supplierid) {
		List<SupplierAnnex> list = ss.listById(supplierid);
		return JSON.toJSONString(list);
	}

	/**
	 * 新增供应商附件
	 * 
	 * @param session
	 * @param supplierid
	 *            供应商ID
	 * @param request
	 * @param protocoenclosurename
	 *            附件文件
	 * @param annex
	 *            供应商附件实体类自动封装
	 * @return 返回json字符串
	 * @throws Exception
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addannex(HttpSession session, Integer supplierid, HttpServletRequest request,
			MultipartFile protocoenclosurename, SupplierAnnex annex) throws Exception {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("上传供应商附件") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		String fileName = protocoenclosurename.getOriginalFilename();
		String filePath = fileName.substring(0, fileName.lastIndexOf(".")) + System.currentTimeMillis()
				+ fileName.substring(fileName.lastIndexOf("."));
		protocoenclosurename.transferTo(new File(FileUtil.SUPPLIER_ANNEX_PATH + filePath));
		annex.setSupplierid(supplierid);
		filePath = filePath.replace("\\", "/");
		annex.setAnnexpath(filePath);
		annex.setAnnexname(protocoenclosurename.getOriginalFilename());
		int a = ss.insertSelective(annex);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 删除供应商附件
	 * 
	 * @param session
	 * @param request
	 * @param path
	 *            供应商附件名称
	 * @param supplierannexid
	 *            供应商附件ID
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deleteannex(HttpSession session, HttpServletRequest request, String path, Integer supplierannexid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除供应商附件") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = ss.deleteByPrimaryKey(supplierannexid);
		FileUtil.delete(FileUtil.SUPPLIER_ANNEX_PATH + path);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 下载供应商附件
	 * 
	 * @param session
	 * @param response
	 * @param path
	 *            供应商附件名称
	 * @param supplierannexid
	 *            供应商附件ID
	 * @throws Exception
	 */
	@RequestMapping("download")
	public void downloadAnnex(HttpSession session, HttpServletResponse response, String path, Integer supplierannexid)
			throws Exception {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("上传供应商附件") != SUCCESS) {
			result.setResult("noRight");
			response.getOutputStream().write("您没有权限下载附件".getBytes());
			return;
		}
		SupplierAnnex annex = ss.selectByPrimaryKey(supplierannexid);
		FileUtil.download(response, FileUtil.SUPPLIER_ANNEX_PATH + path, annex.getAnnexname());
	}

}
